
#include "LineGraphWidget.h"

LineGraphWidget::LineGraphWidget(int mix, int max, QWidget *parent) :

    QWidget(parent),

    rangeMin(mix),

    rangeMax(max),

    singleUnitX(5),

    timer(NULL)
{

    connect(this,SIGNAL(newDataCome()),this,SLOT(update()));
    setWindowTitle("1s单位时间数据流量图");
//    setWindowFlags(Qt::FramelessWindowHint);

}


void LineGraphWidget::set(int mix,int max){
    rangeMax=max;
    rangeMin=mix;

}


void LineGraphWidget::paintEvent(QPaintEvent *)                      // timer trigger

{

    int i;

    int a,b,c,d;

    Counter = 0;

    QPixmap pix(widgetX,widgetY);


    QPainter painter(&pix);

    pix.fill(Qt::white);

    QPen pen;

    pen.setBrush(Qt::darkGreen);

    painter.setPen(pen);

    QPen pen1;

    pen1.setBrush(Qt::black);

    QPen pen2;

    pen2.setBrush(Qt::red);

    QPen pen3;

    pen3.setBrush(Qt::yellow);

    painter.setRenderHint(QPainter::Antialiasing);

    for(i = 0;i < sumUnitX; ++i) {

        a = basePoint.x + i*singleUnitX;

        b = basePoint.y;

        c= a;

        d = yAxisEndPoint.y;

        painter.drawLine(a,b,c,d);   //vertecal line

    }

    for(i = 0; i < sumUnitY+1; ++i) {

        a = basePoint.x;

        b = basePoint.y - singleUnitY*i;

        c = xAxisEndPoint.x;

        d = b;

        painter.drawLine(a,b,c,d);  //horizontal line

    }

    painter.setPen(pen1);

    for (i = 0;i < (sumUnitY/2+1);++i) { //pre 2 singleY drawText

        QRectF rectF(0,basePoint.y - singleUnitY*i*2 - 5,basePoint.x,(widgetY - basePoint.y)/3); //string rect   up y 2singleUnitY

        painter.drawText(rectF,Qt::AlignCenter,QString::number(rangeMin+i*10));//20 string label   add 10

    }

    for (i = 0;i < 6; ++i) { //pre 2 singleY drawText

        QRectF rectF(widgetX*0.4+50,basePoint.y*0.5 - singleUnitY*i*5 - 5,widgetX,(widgetY - basePoint.y)/3); //string rect   up y 2singleUnitY
        switch(i){
        case 0 :
            painter.setPen(pen3);
            painter.drawText(rectF,Qt::AlignCenter,"UDP");
            break;
        case 1 :
            painter.setPen(pen3);
            painter.drawLine(widgetX*0.9+40,basePoint.y*0.5 - singleUnitY*i*5+18 ,widgetX*0.9+60,basePoint.y*0.5 - singleUnitY*i*5+18);
            break;
        case 2 :
            painter.setPen(pen1);
            painter.drawText(rectF,Qt::AlignCenter,"TCP");
            break;
        case 3:
            painter.setPen(pen1);
            painter.drawLine(widgetX*0.9+40,basePoint.y*0.5 - singleUnitY*i*5+18 ,widgetX*0.9+60,basePoint.y*0.5 - singleUnitY*i*5+18);
            break;
        case 4:
            painter.setPen(pen2);
            painter.drawText(rectF,Qt::AlignCenter,"IP");
            break;
        case 5:
            painter.setPen(pen2);
            painter.drawLine(widgetX*0.9+40,basePoint.y*0.5 - singleUnitY*i*5+18 ,widgetX*0.9+60,basePoint.y*0.5 - singleUnitY*i*5+18);
            break;
        default:
            break;

        }
        //20 string label   add 10

    }

    painter.setPen(pen1);

    for (i = 0;i < sumUnitX/5;++i) {  //pre 5 singleX drawText

        QRectF rectF(basePoint.x+singleUnitX*(5*i+3),basePoint.y,singleUnitX*4,widgetY-basePoint.y);  //string rect

        painter.drawText(rectF,Qt::AlignCenter,QString::number(1*(i+1)*singleUnitX)); //5 string label add singleX

    }



    // read list update,  very important          TCP

    int size = xList.size();

    while(Counter < (size -1)) {

//        painter.drawLine(basePoint.x + xList.value(Counter)*singleUnitX,

//                         basePoint.y - ((yList.value(Counter) - rangeMin)/10.0)*singleUnitY,

//                         basePoint.x + xList.value(Counter + 1)*singleUnitX,

//                         basePoint.y - ((yList.value(Counter + 1) - rangeMin)/10.0)*singleUnitY);
            painter.drawLine(basePoint.x + Counter*singleUnitX,

                             basePoint.y - ((yList.value(Counter) - rangeMin)/5.0)*singleUnitY,

                             basePoint.x + (Counter + 1)*singleUnitX,

                             basePoint.y - ((yList.value(Counter + 1) - rangeMin)/5.0)*singleUnitY);

        Counter++;

    }

//    painter.end();



    // read iplist update,  very important          IP

//    QPainter painter2 (&pix);



    painter.setPen(pen2);

    size = xipList.size();

    while(Counterip < (size -1)) {

            painter.drawLine(basePoint.x + Counterip*singleUnitX,

                             basePoint.y - ((yipList.value(Counterip) - rangeMin)/5.0)*singleUnitY,

                             basePoint.x + (Counterip + 1)*singleUnitX,

                             basePoint.y - ((yipList.value(Counterip + 1) - rangeMin)/5.0)*singleUnitY);

        Counterip++;

    }

    //  udp   show


    painter.setPen(pen3);

    size = xudpList.size();

    while(Counterudp < (size -1)) {

            painter.drawLine(basePoint.x + Counterudp*singleUnitX,

                             basePoint.y - ((yudpList.value(Counterudp) - rangeMin)/5.0)*singleUnitY,

                             basePoint.x + (Counterudp + 1)*singleUnitX,

                             basePoint.y - ((yudpList.value(Counterudp + 1) - rangeMin)/5.0)*singleUnitY);

        Counterudp++;

    }


    painter.end();

    painter.begin(this);

    painter.drawPixmap(0,0,pix);

    painter.end();

    Counter = 0; Counterip=0;Counterudp=0;


}

void LineGraphWidget::resizeEvent(QResizeEvent * e)

{

    if (e->oldSize() != size()) {

        initGraph();

    }

}

void LineGraphWidget::showData(const int &data)         //only insert , update in  paintEvent()
{

    //这里还要进行相关的数据正确性检测，不能超过图的范围

    if (data > rangeMax || data < rangeMin) {

        return;

    }

    int xsize = xList.size();

    int yValue = data;

    if (xsize == sumUnitX) {

        xList.clear();

        yList.clear();

        xsize = xList.length();

    }

    if (xsize >= 1) {

        xList.insert(xsize,xsize);

        yList.insert(xsize,yValue);

        emit newDataCome();

    }

    else {

        xList.insert(xsize,xsize);

        yList.insert(xsize,yValue);

    }

}


void LineGraphWidget::showipData(const int &data){

    if (data > rangeMax || data < rangeMin) {

        return;

    }

    int xsize = xipList.size();

    int yValue = data;

    if (xsize == sumUnitX) {

        xipList.clear();

        yipList.clear();

        xsize = xipList.length();

    }

    if (xsize >= 1) {

        xipList.insert(xsize,xsize);

        yipList.insert(xsize,yValue);

        emit newDataCome();

    }

    else {

        xipList.insert(xsize,xsize);

        yipList.insert(xsize,yValue);

    }

}

void LineGraphWidget::showudpData(const int &data){
    if (data > rangeMax || data < rangeMin) {

        return;

    }

    int xsize = xudpList.size();

    int yValue = data;

    if (xsize == sumUnitX) {

        xudpList.clear();

        yudpList.clear();

        xsize = xudpList.length();

    }

    if (xsize >= 1) {

        xudpList.insert(xsize,xsize);

        yudpList.insert(xsize,yValue);

        emit newDataCome();

    }

    else {

        xudpList.insert(xsize,xsize);

        yudpList.insert(xsize,yValue);

    }

}

void LineGraphWidget::getSumUnitX()

{

    if ((static_cast<int>(widgetX*0.85))%singleUnitX == 0){

        sumUnitX = (static_cast<int>(widgetX*0.85))/singleUnitX;

    }

    else {

        sumUnitX = (static_cast<int>(widgetX*0.85))/singleUnitX + 1;

    }

}

void LineGraphWidget::getSingleUnitY()

{

    int range = rangeMax - rangeMin;

    if (range == 0) {

        sumUnitY = range/10 + 1;

    }

    else {

        sumUnitY = range/10 + 2;

    }

    singleUnitY = (static_cast<int>(widgetY*0.85))/sumUnitY;

}

void LineGraphWidget::initGraph()

{

    widgetX = size().width();

    widgetY = size().height();

    basePoint.x = static_cast<int>(widgetX*0.1);

    basePoint.y = static_cast<int>(widgetY*0.9);

    yAxisEndPoint.x = basePoint.x;

    yAxisEndPoint.y = static_cast<int>(widgetY*0.05);

    xAxisEndPoint.x = static_cast<int>(widgetX*0.95);

    xAxisEndPoint.y = basePoint.y;

    getSumUnitX();

    getSingleUnitY();

}

void LineGraphWidget::startUpdataTimer(int msecond)

{

    if (timer != NULL) {

        if (timer->isActive()) {

            timer->stop();

        }

        delete timer;

    }

    disconnect(SIGNAL(newDataCome()));

    timer = new QTimer;

    connect(timer,SIGNAL(timeout()),this,SLOT(update()));

    timer->start(msecond);

}

void LineGraphWidget::stopUpdataTimer()

{

    if (timer == NULL) {

    }

    else {

        timer->stop();

        delete timer;

    }

}
